/*
 * Created by Jaren at 2022/10/28 17:42
 */
import 'dart:typed_data';

import 'package:binary/binary.dart';
import 'package:inmotion_protocol/common/compact.dart';
import 'package:inmotion_protocol/common/product_model.dart';
import 'package:inmotion_protocol/common/product_version.dart';
import 'package:inmotion_protocol/lorin/bean/scooter/scooter_settings.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

class LE1Settings extends ScooterSettings {
  Uint8 frontBrakeForce = Uint8.zero;
  Uint8 rearBrakeForce = Uint8.zero;

  LE1Settings(ProductModel model, VersionWrapper version) : super(model, version);

  @override
  void initCompact() {
    compact = DefaultLE1Settings();
  }

  /// LE1骑行模式定义不同
  /// see [LE1MessageFactory.genSetRidingModeMsg]
  @override
  RidingMode toRidingMode(int mode) {
    switch (mode) {
      case 1:
        return RidingMode.normal;
      case 2:
        return RidingMode.sport;
      case 0:
      default:
        return RidingMode.assist;
    }
  }
}

class DefaultLE1Settings extends Compact<LE1Settings> {
  @override
  void fromBytes(LE1Settings output, Uint8ClampedList bytes, int offset) {
    output.zeroStartupState = ByteUtil.bitValue(bytes[offset], 0, 1);
    output.cruiseState = ByteUtil.bitValue(bytes[offset], 1, 1);
    output.speedUnit = ByteUtil.bitValue(bytes[offset], 2, 1);
    output.ridingMode = output.toRidingMode(ByteUtil.bitValue(bytes[offset], 3, 3).value);
    output.lockMode = ByteUtil.bitValue(bytes[offset], 6, 1);
    output.safeSpeedLimitState = ByteUtil.bitValue(bytes[offset], 7, 1);
    offset++;

    offset++; //front acc force
    offset++; //rear acc force

    output.frontBrakeForce = ByteUtil.toUint8(bytes[offset]);
    offset++;
    output.rearBrakeForce = ByteUtil.toUint8(bytes[offset]);

    // 使用后电机刹车力度作为综合刹车力度
    output.brakeForce = output.rearBrakeForce;
    // 其余设置项暂时先不实现
  }

  @override
  int length() {
    return 6;
  }
}
